<template>
  <div class="login_container">
    <div class="login_box">
      <!--  头像  -->
      <div class="logo">
        <img src="../assets/logo.png" alt="">
      </div>
      <!--  登录表单  -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-live-spiker"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-lock"></el-input>
        </el-form-item>
        <el-form-item label-width="0px" class="form_btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: "Login.vue",
    data() {
      return {
        /*  3.登录表单数据绑定对象  */
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        loginFormRules: {
          username: [
            {required: true, message: '请输入用户名称', trigger: 'blur'},
            {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
          ]
        }

      }
    },
    methods: {
      /*  重置表单 */
      resetForm() {
        this.$refs.loginFormRef.resetFields();
      },
      login() {
        this.$refs.loginFormRef.validate(async (valid) => {
          if (!valid) return;
          const {data : res} = await this.$http.post('login',this.loginForm);
          // console.log(res);
          if (res.meta.status === 200){
            this.$message.success('登录成功');
            window.sessionStorage.setItem("token",res.data.token);
            await this.$router.push('/home')
          }else{
            this.$message.error('登录失败');
          }
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .login_container {
    width: 100%;
    height: 100%;
    background-color: #2b4b6b;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .login_box {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: white;
    position: relative;
    @h: 130px;
    /*  头像  */

    .logo {
      width: @h;
      height: @h;
      border-radius: 50%;
      margin: -(@h / 2) auto;
      background-color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid #eee;
      box-shadow: 0 0 10px #eee;

      img {
        width: 90%;
        height: 90%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    /*  登录表单  */

    .el-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 20px;
      box-sizing: border-box;

      .form_btn {
        float: right;
      }
    }
  }
</style>
